package com.summer.data.jql.concept.func;

import com.summer.data.Jsql;
import com.summer.data.dialect.Dialect;
import com.summer.data.jql.Context;
import com.summer.data.jql.concept.Field;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 345480567@qq.com
 */
public class Concat implements Field {

    List<Field> fieldList;

    public Concat(Field... fields) {
        if (fields.length > 0 && fieldList == null) {
            fieldList = new ArrayList<>(Arrays.asList(fields));
        }
    }

    @Override
    public Jsql toFieldSql(Context ctx, Dialect dialect) {
        Jsql jsql = new Jsql();
        jsql.append("CONCAT(");
        for (int i = 0; i < fieldList.size(); i++) {
            Field field = fieldList.get(i);
            if (i != 0) {
                jsql.append(",");
            }
            jsql.append(field.toFieldSql(ctx, dialect));
        }
        jsql.append(")");
        return jsql;
    }

}
